/**
 * 
 */
package org.xlab.baseframework.web.component;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.vaadin.peter.buttongroup.ButtonGroup;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.domain.SystemCode;
import org.xlab.baseframework.domain.view.SystemCodeVO;
import org.xlab.baseframework.exception.SystemCodeDuplicateException;
import org.xlab.baseframework.exception.SystemCodeNotFoundException;
import org.xlab.baseframework.service.api.ISystemCodeService;
import org.xlab.baseframework.web.data.CodeAddFormFieldFactory;

import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * @author MaKuBEX
 * 
 */
@Controller("bpf_codeAddWindow")
@Scope("request")
public class CodeAddWindow extends Window {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 1245518584216566843L;
	private Form addForm;
	private Container uiContainer;
	private Button save;
	private Button close;
	private ButtonGroup buttons;

	@Autowired(required = true)
	@Qualifier("bpf_systemCodeService")
	private ISystemCodeService systemCodeService;
	@Autowired
	@Qualifier("bpf_codeAddFormFieldFactory")
	private CodeAddFormFieldFactory codeAddFormFieldFactory;

	/**
	 * @param caption
	 */
	public CodeAddWindow(String caption) {
		super(caption);
		super.setModal(true);
		super.setClosable(true);
		super.addListener(new CloseListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 5737479534375044266L;

			public void windowClose(CloseEvent e) {
				CodeAddWindow.super.removeAllComponents();
				CodeAddWindow.this.addForm = null;
				CodeAddWindow.this.save = null;
				CodeAddWindow.this.close = null;
			}
		});
	}

	/**
	 * @see com.vaadin.ui.Window#attach()
	 */
	public void attach() {
		super.attach();
		VerticalLayout mainLayout = new VerticalLayout();
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		mainLayout.setSizeFull();
		this.addForm = new Form();
		this.addForm.setWriteThrough(false);
		this.addForm.setInvalidCommitted(false);
		this.addForm.setImmediate(false);
		this.addForm.setWidth(300f, UNITS_PIXELS);
		this.addForm.setItemDataSource(new BeanItem<SystemCodeVO>(
				new SystemCodeVO()));
		this.addForm.setFormFieldFactory(this.codeAddFormFieldFactory);
		this.addForm.setVisibleItemProperties(Arrays.asList(new String[] {
				SystemCodeVO.FIELD_CATEGORY, SystemCodeVO.FIELD_CODE,
				SystemCodeVO.FIELD_DESCRIPTION, SystemCodeVO.FIELD_SEQUENCE }));
		this.buttons = new ButtonGroup();
		this.save = new Button("Save");
		this.save.addListener(new ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -4866313876467653755L;

			@SuppressWarnings("unchecked")
			public void buttonClick(ClickEvent event) {
				addForm.commit();
				SystemCodeVO codeVO = ((BeanItem<SystemCodeVO>) addForm
						.getItemDataSource()).getBean();
				SystemCode category = null;
				try {
					if (codeVO.getCategory() != null)
						category = CodeAddWindow.this.systemCodeService
								.findByCode(codeVO.getCategory().getCode(),
										true);
					systemCodeService.createSystemCode(codeVO.getCode(), codeVO
							.getDescription(), codeVO.getSequence(), category,
							(ISystemUserDomain) CodeAddWindow.super
									.getApplication().getUser());
					if (CodeAddWindow.this.uiContainer != null) {
						codeVO = systemCodeService
								.convertToVO(systemCodeService.findByCode(
										codeVO.getCode(), true));
						CodeAddWindow.this.setData(codeVO);
					}
					CodeAddWindow.this.close();
				} catch (SystemCodeNotFoundException e) {
					e.printStackTrace();
				} catch (SystemCodeDuplicateException e) {
					CodeAddWindow.super
							.getWindow()
							.showNotification(
									"Could not add this code",
									"You can not create this code because there is a system code with the same code!",
									Notification.TYPE_ERROR_MESSAGE);
				}
			}
		});
		this.close = new Button("Close");
		this.close.addListener(new ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 8644327937684401707L;

			public void buttonClick(ClickEvent event) {
				CodeAddWindow.this.close();
			}
		});
		this.buttons.addButton(save);
		this.buttons.addButton(close);
		mainLayout.addComponent(this.addForm);
		mainLayout.addComponent(this.buttons);
		mainLayout.setComponentAlignment(this.addForm, Alignment.TOP_LEFT);
		mainLayout.setComponentAlignment(this.buttons, Alignment.MIDDLE_CENTER);
		super.addComponent(mainLayout);
	}

	/**
	 * @param systemCodeService
	 *            the systemCodeService to set
	 */
	public void setSystemCodeService(ISystemCodeService systemCodeService) {
		this.systemCodeService = systemCodeService;
	}

	/**
	 * @param codeAddFormFieldFactory
	 *            the codeAddFormFieldFactory to set
	 */
	public void setCodeAddFormFieldFactory(
			CodeAddFormFieldFactory codeAddFormFieldFactory) {
		this.codeAddFormFieldFactory = codeAddFormFieldFactory;
	}

	/**
	 * @param uiContainer
	 *            the uiContainer to set
	 */
	public void setUiContainer(Container uiContainer) {
		this.uiContainer = uiContainer;
	}

}
